class Solution:
    def maxCoins(self, piles: list) -> int:
        counter = [0 for _ in range(10**4 + 1)]
        for pile in piles:
            counter[pile] += 1
        p = 0
        for i in range(10**4 + 1):
            while counter[i] > 0:
                piles[p] = i
                p += 1
                counter[i] -= 1

        ans = 0
        while piles:
            piles.pop()
            ans += piles.pop()
            piles.pop(0)

        return ans
